package com.ruoyi.prod.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.prod.entity.po.ProdAsset;
import com.ruoyi.prod.entity.po.ProdPurchase;
import com.ruoyi.prod.entity.po.ProdUseApply;
import com.ruoyi.prod.entity.po.ProdUseApplyItem;
import com.ruoyi.prod.enums.AssetState;
import com.ruoyi.prod.enums.LogModuleEnum;
import com.ruoyi.prod.enums.LogTypeEnum;
import com.ruoyi.prod.enums.OddTypeEnum;
import com.ruoyi.prod.mapper.ProdUseApplyMapper;
import com.ruoyi.prod.service.BaseLogService;
import com.ruoyi.prod.service.ProdAssetService;
import com.ruoyi.prod.service.ProdUseApplyItemService;
import com.ruoyi.prod.service.ProdUseApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.prod.util.IsUtil;
import com.ruoyi.prod.util.OddUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 使用管理-资产使用 服务实现类
 * </p>
 *
 * @author 强子初
 * @since 2023-02-25
 */
@Service
public class ProdUseApplyServiceImpl extends ServiceImpl<ProdUseApplyMapper, ProdUseApply> implements ProdUseApplyService {

    @Autowired
    ProdUseApplyItemService itemService;

    @Autowired
    ProdAssetService assetService;

    @Autowired
    BaseLogService logService;

    @Override
    public String getOdd() {
        return OddUtil.generate(OddTypeEnum.USE_APPLY.getCode());
    }

    @Override
    public ProdUseApply get(String id) {
        ProdUseApply po = this.getById(id);
        return this.get(po);
    }

    @Override
    public ProdUseApply get(ProdUseApply po) {
        po.setItem(itemService.list(po.getId()));
        return po;
    }

    @Override
    public List<ProdUseApply> list(ProdUseApply condition) {
        List<ProdUseApply> list = this.list(this.getWrapper(condition));
        list = list.stream().map(this::get).collect(Collectors.toList());
        return list;
    }

    @Override
    public boolean add(ProdUseApply po) {
        po.setAuditState(IsUtil.getAuditState(po.getAction()));
        po.setOdd(this.getOdd());
        this.save(po);
        // 添加具体项
        itemService.add(po, po.getItem());

        return true;
    }
    @Override
    public void auditPass(ProdUseApply po) {
        // 获取
        po = this.get(po.getId());
        // 修改子项信息
        List<ProdUseApplyItem> items = po.getItem();
        items = items.stream()
                .peek((item) -> item.setUseState("2"))
                .collect(Collectors.toList());
        itemService.saveOrUpdateBatch(items);
        // 修改台账信息
        List<ProdAsset> assetList = items.stream()
                .map((item) -> {
                    ProdAsset asset = assetService.getById(item.getAssetId());
                    asset.setAssetState(AssetState.IN_USE);
                    return asset;
                })
                .collect(Collectors.toList());
        assetService.saveOrUpdateBatch(assetList);
        // 增加资产使用日志
        List<String> ids = assetList.stream().map(ProdAsset::getId).collect(Collectors.toList());
        logService.saveBatch(ids, LogModuleEnum.ASSET, LogTypeEnum.USE);
    }

    private LambdaQueryWrapper<ProdUseApply> getWrapper(ProdUseApply condition) {
        LambdaQueryWrapper<ProdUseApply> wrapper = new LambdaQueryWrapper<>();

        if (condition.getStateList() != null) {
            wrapper.in(ProdUseApply::getAuditState, condition.getStateList());
        }
        return wrapper;
    }
}
